/*
 * This file is part of Neo Launcher
 * Copyright (c) 2024   Neo Launcher Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 */

package com.saggitt.omega.gestures.handlers

import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity

class SleepTimeoutActivity : AppCompatActivity() {

    private val timeout by lazy {
        Settings.System.getInt(
            contentResolver,
            Settings.System.SCREEN_OFF_TIMEOUT,
            60000
        )
    }
    private val stayOnWhilePluggedIn by lazy {
        Settings.System.getInt(
            contentResolver,
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
            0
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        timeout
        stayOnWhilePluggedIn

        window.decorView.systemUiVisibility =
            window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

        putSettings(0, 0)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)

        if (!hasFocus) {
            finish()
        }
    }

    override fun onPause() {
        super.onPause()

        finish()
    }

    override fun onDestroy() {
        super.onDestroy()

        putSettings(timeout, stayOnWhilePluggedIn)
    }

    private fun putSettings(timeout: Int, stayOnWhilePluggedIn: Int) {
        if (!Settings.System.canWrite(this)) return
        Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, timeout)
        Settings.System.putInt(
            contentResolver,
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
            stayOnWhilePluggedIn
        )
        Log.d(
            "SleepTimeoutActivity",
            "Screen timeout settings set to $timeout $stayOnWhilePluggedIn"
        )
    }
}